<template>
    <div id="dayWorkList">
      <el-dialog title="日报查看" :visible.sync="dialogFormVisibleView">
        <el-form>
          <el-form-item class="mb0" label="日期：" :label-width="formLabelWidth">
            <el-col :span="24">
              <p class="text-left">{{ dateView }}</p>
            </el-col>
          </el-form-item>
          <el-form-item class="mb0" label="项目组：" :label-width="formLabelWidth">
            <el-col :span="24">
              <p class="text-left">{{ groupView }}</p>
            </el-col>
          </el-form-item>
          <el-form-item class="mb0" label="完成任务：" :label-width="formLabelWidth">
            <el-col :span="24">
              <p class="text-left">{{ yestodaySuccessView }}</p>
            </el-col>
          </el-form-item>
          <el-form-item class="mb0" label="未完成：" :label-width="formLabelWidth">
            <el-col :span="24">
              <p class="text-left">{{ yestodayFailView }}</p>
            </el-col>
          </el-form-item>
          <el-form-item class="mb0" label="输出成果：" :label-width="formLabelWidth">
            <el-col :span="24">
              <p class="text-left">{{ resultView }}</p>
            </el-col>
          </el-form-item>
          <el-form-item class="mb0" label="今日计划：" :label-width="formLabelWidth">
            <el-col :span="24">
              <p class="text-left">{{ todayPlanView }}</p>
            </el-col>
          </el-form-item>
          <el-form-item class="mb0" label="需要支持：" :label-width="formLabelWidth">
            <el-col :span="24">
              <p class="text-left">{{ helpView }}</p>
            </el-col>
          </el-form-item>
        </el-form>
      </el-dialog>
      <el-dialog title="日报修改" :visible.sync="dialogFormVisible">
        <el-form :label-position="labelPosition" :rules="rules" ref="form" :model="form">
          <el-form-item label="选择日期" prop="date" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-date-picker :disabled="codeBtype" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" v-model="form.date" style="width: 100%;"></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="项目组" prop="group" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-select :disabled="codeBtype" style="width: 100%;" v-model="form.group" clearable placeholder="请选择项目组">
                <el-option
                  v-for="item in groupOptionsOnly"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="完成任务" prop="yestodaySuccess" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input :disabled="codeBtype" type="textarea" :rows="3" v-model="form.yestodaySuccess"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="未完成" prop="yestodayFail" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input :disabled="codeBtype" type="textarea" :rows="3" v-model="form.yestodayFail"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="输出成果" prop="result" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input :disabled="codeBtype" type="textarea" :rows="3" v-model="form.result"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="今日计划" prop="todayPlan" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input :disabled="codeBtype" type="textarea" :rows="3" v-model="form.todayPlan"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="需要支持" prop="help" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input :disabled="codeBtype" type="textarea" :rows="3" v-model="form.help"></el-input>
            </el-col>
          </el-form-item>
        </el-form>
        <div v-if="codeBtype" slot="footer" class="dialog-footer"></div>
        <div v-else slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="formSubmit('form')">确 定</el-button>
          <!-- <el-button @click="resetForm('form')">重置</el-button> -->
        </div>
      </el-dialog>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="姓名">
          <el-select v-model="formInline.user" clearable placeholder="请选择姓名" @change="getTableList">
            <el-option
              v-for="item in nameOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="项目组">
          <el-select v-model="formInline.group" clearable placeholder="请选择项目组" @change="getTableList">
            <el-option
              v-for="item in groupOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="选择日期">
          <el-date-picker @change="getTableList" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" v-model="formInline.date" style="width: 100%;"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    	<el-table
          :data="tableData"
          border
          style="width: 100%">
          <el-table-column
            prop="daily_date"
            label="日期"
            align="center"
            width="180">
          </el-table-column>
          <el-table-column
            prop="user_name"
            label="姓名"
            align="center"
            width="180">
          </el-table-column>
          <el-table-column
            prop="group_name"
            label="项目组"
            align="center"
            width="180">
          </el-table-column>
          <el-table-column
            prop="completed_task"
            label="完成任务"
            align="center">
          </el-table-column>
          <el-table-column
            prop="uncompleted_task"
            label="未完成任务"
            align="center">
          </el-table-column>
          <el-table-column
            prop="output_result"
            label="输出成果物"
            align="center">
          </el-table-column>
          <el-table-column
            prop="today_plan"
            label="今日计划"
            align="center">
          </el-table-column>
          <el-table-column
            prop="need_help_content"
            label="需要支持"
            align="center">
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="200">
            <template slot-scope="scope">
              <el-button v-if="showLook" @click="handleViewClick(scope.row)" type="text" size="small">查看</el-button>
              <el-button v-if="showModify" @click="handleClick(scope.row)" type="text" size="small">修改</el-button>
              <el-button v-if="showDel" @click.native.prevent="deleteRow(scope.$index, tableData)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[15, 25, 50]"
            :page-size="perPage"
            layout="total, sizes,prev, pager, next,jumper"
            :total="total">
          </el-pagination>
        </div>
        <el-input v-model="rowId" type="hidden"></el-input>
    </div>
</template>
<script>
export default {
  name: 'dayWorkList',
  data () {
    return {
      formInline: {
        user: '',
        //group: '',
        date: ''
      },
      form: {
        group: '',
        date: '', 
        yestodaySuccess: '',
        yestodayFail: '',
        result: '',
        todayPlan: '',
        help: ''
      },
      rules: {
        date: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ],
        group: [
          { required: true, message: '请选择项目组', trigger: 'change' }
        ],
        yestodaySuccess: [
          { required: true, message: '请输入已完成任务', trigger: 'blur' }
        ],
        yestodayFail: [
          { required: true, message: '请输入未完成任务', trigger: 'blur' }
        ],
        result: [
          { required: true, message: '请输入输出成果', trigger: 'blur' }
        ],
        todayPlan: [
          { required: true, message: '请输入今日计划', trigger: 'blur' }
        ],
        help: [
          { required: true, message: '请输入需要支持', trigger: 'blur' }
        ]
      },
      groupView:'',
      dateView:'',
      yestodaySuccessView:'',
      yestodayFailView:'',
      resultView:'',
      todayPlanView:'',
      helpView:'',
      codeBtype:false,
      labelPosition: 'right',
      rowId:'',
      formLabelWidth: '260px',
      dialogFormVisible:false,
      dialogFormVisibleView:false,
      nameOptions:[],
      //groupOptions:[],
      groupOptionsOnly:[],
      currentPage: 1,
      perPage: 15,
      //总页数
      total: null,
      tableData: []
    };
  },
  created() {
    this.getTableList();
    this.getNameList();
    //this.getGroupList();
    this.rootSelect();
  },
  beforeDestroy() {},
  computed: {},
  mounted() {},
  methods: {
    onSubmit() {
      var self = this;
      self.getTableList();
    },
    handleSizeChange(val) {
      //console.log(`每页 ${val} 条`);
      var self = this;
      self.perPage = val;
      self.getTableList();
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`);
      var self = this;
      self.currentPage = val;
      self.getTableList();
    },
    getTableList() {
      var self = this;
      //console.log(self.currentPage)
      if(self.formInline.date == null){
        self.formInline.date ='';
      }
      self.getRequest("/daily/queryDaily?page_no=" +self.currentPage+'&&page_size='+self.perPage
      +'&&user_id='+self.formInline.user+'&&daily_date='+self.formInline.date+'&&group_id='+this.defines.globalValueGroup
      , {}).then(
        res => {      
          console.log(res.data)     
          self.tableData = res.data.data;
          self.total = res.data.recordsTotal;
          self.perPage = res.data.length;
        }
      );
    },
    getNameList() {
      
      var self = this;
      self.nameOptions = [];
      self.getRequest("/base/common/queryUserNameByGroupId?group_id="+this.defines.globalValueGroup, {}).then(
        res => {      
          console.log(res.resultData)     
          var obj = res.data.resultData;
            var arr = [];
            for(var i in obj) {
              var row = {};
              row.label = obj[i].name;
              row.value = obj[i].id;
              arr.push(row);
            }
            //console.log(arr)
            self.nameOptions = arr;
        }
      );
    },
    // getGroupList() {
    //   var self = this;
    //   //console.log(self.currentPage)
    //   self.getRequest("/base/common/queryProjectName?flug="+2, {}).then(
    //     res => {      
    //       console.log(res.resultData)     
    //       var obj = res.data.resultData;
    //         var arr = [];
    //         for(var i in obj) {
    //           var row = {};
    //           row.label = obj[i].pName;
    //           row.value = obj[i].id;
    //           arr.push(row);
    //         }
    //         //console.log(arr)
    //         self.groupOptions = arr;
    //     }
    //   );
    // },
    getGroupListOnly() {
      var self = this;
      //console.log(self.currentPage)
      self.getRequest("/base/common/queryProjectName?flug="+1, {}).then(
        res => {      
          console.log(res.resultData)     
          var obj = res.data.resultData;
            var arr = [];
            for(var i in obj) {
              var row = {};
              row.label = obj[i].pName;
              row.value = obj[i].id;
              arr.push(row);
            }
            //console.log(arr)
            self.groupOptionsOnly = arr;
        }
      );
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //修改
    handleClick(row) {
      //console.log(row);

      var self = this;
      //console.log(row.user_id+'dddd')
      self.postRequest("/daily/compareUserIdAndLoginUser", {
          user_id:row.user_id
        }).then(
          res => {      
            console.log(res)     
            if(res.data.result == '1' ){            
              //console.log(self.codeBtype)
              self.codeBtype = false;
              self.getGroupListOnly();
              self.rowId = row.id;
              self.dialogFormVisible = true;
              self.form.group = row.group_id;
              self.form.date = row.daily_date;
              self.form.yestodaySuccess = row.completed_task;
              self.form.yestodayFail = row.uncompleted_task;
              self.form.result = row.output_result;
              self.form.todayPlan = row.today_plan;
              self.form.help = row.need_help_content;
            }else if(res.data.result == '2' ){
              this.$message({
                  message: res.data.message,
                  type: 'error'
              });
            }
            
          }
        );
      
      
    },
    //查看
    handleViewClick(row) {
      console.log(row);
      var self = this;
      self.rowId = row.id;
      self.dialogFormVisibleView = true;
      self.groupView = row.group_name;
      self.dateView = row.daily_date;
      self.yestodaySuccessView = row.completed_task;
      self.yestodayFailView = row.uncompleted_task;
      self.resultView = row.output_result;
      self.todayPlanView = row.today_plan;
      self.helpView = row.need_help_content;
    },
    formSubmit(formName) {     
      this.$refs[formName].validate((valid) => {
          if (valid) {
            var self = this;            
            self.postRequest("/daily/updateDaily", {
              id:self.rowId,
              group_id:self.form.group,
              daily_date:self.form.date,
              completed_task:self.form.yestodaySuccess,
              uncompleted_task:self.form.yestodayFail,
              output_result:self.form.result,
              today_plan:self.form.todayPlan,
              need_help_content :self.form.help
            }).then(
              res => {      
                console.log(res)     
                if(res.data.result == '1' ){            
                  this.$message({
                      message: res.data.message,
                      type: 'success'
                  });
                }else if(res.data.result == '2' ){
                  this.$message({
                      message: res.data.message,
                      type: 'error'
                  });
                }
                self.dialogFormVisible = false;
                self.getTableList();
              }
            );
          }
        })
      
      
    },
    deleteRow(index, rows) {
      console.log(rows[index])
      var self = this;
      self.
        postRequest("/daily/confirmUserDeletePremission", {
          user_id:rows[index].user_id
        }).then(
          res => {      
            console.log(res.data) 
            if(res.data.result == '1' ){            
              // this.$message({
              //     message: res.data.message,
              //     type: 'success'
              // });
              self.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                  self.
                    postRequest("/daily/deleteDaily", {
                      id:rows[index].id

                    }).then(
                      res => {      
                        console.log(res.data) 
                        if(res.data.result == '1' ){            
                          this.$message({
                              message: res.data.message,
                              type: 'success'
                          });
                        }else if(res.data.result == '2' ){
                          this.$message({
                              message: res.data.message,
                              type: 'error'
                          });
                        }    
                        self.getTableList();
                      }
                    );
                }).catch(() => {
                  self.$message({
                    type: 'info',
                    message: '已取消删除'
                  });          
                });



            }else if(res.data.result == '2' ){
              this.$message({
                  message: res.data.message,
                  type: 'error'
              });
            }    
            //self.getTableList();
          }
        );
      
      
    },
    rootSelect() {
      var self = this;            
        self.getRequest("/currCrudPermissions", {

        }).then(
          res => {      
            //console.log(typeof res.data)     
            var arr = [];
            for (var i in res.data){
              arr.push(res.data[i]);
            }
            console.log(arr)
            if (arr.indexOf('修改')>-1){
              self.showModify = true;
            }else{
              self.showModify = false;
            }
            if (arr.indexOf('删除')>-1){
              self.showDel = true;
            }else{
              self.showDel = false;
            }
            if (arr.indexOf('查看')>-1){
              self.showLook = true;
            }else{
              self.showLook = false;
            }
            
          }
        );
    }
  }
}
</script>

<style scoped>
  .demo-form-inline {text-align: left;}
  .el-pagination {margin: 20px 0;}
  .text-left {text-align: left;}
  .mb0 {margin-bottom: 0;}
</style>